package com.duanjw.bank.rate.handler;

import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 广发银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class GuangFaBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.cgbchina.com.cn/showCHYDepositRate.gsp");

        Map<String, String> params = new HashMap<>();
        params.put("infoid", "111111111");
        Document responseDoc = connect.data(params).headers(headers).ignoreContentType(true).method(Connection.Method.POST).get();


        Element table = responseDoc.select("table[class=second_tb]").get(0);

        String kaiShiZhiXingRiQi = responseDoc.selectXpath("body/table[3]/tbody/tr[1]/td[2]").text();

        String huoQiCunKuanLiLv = table.selectXpath("tbody/tr[2]/td[2]").text();

        String zhengCunZhengQu3YueLiLv = table.selectXpath("tbody/tr[5]/td[2]").text();
        String zhengCunZhengQu6YueLiLv = table.selectXpath("tbody/tr[6]/td[2]").text();
        String zhengCunZhengQu12YueLiLv = table.selectXpath("tbody/tr[7]/td[2]").text();
        String zhengCunZhengQu24YueLiLv = table.selectXpath("tbody/tr[8]/td[2]").text();
        String zhengCunZhengQu36YueLiLv = table.selectXpath("tbody/tr[9]/td[2]").text();
        String zhengCunZhengQu60YueLiLv = table.selectXpath("tbody/tr[10]/td[2]").text();

        String lingCunZhengQu12YueLiLv = table.selectXpath("tbody/tr[12]/td[2]").text();
        String lingCunZhengQu36YueLiLv = table.selectXpath("tbody/tr[13]/td[2]").text();
        String lingCunZhengQu60YueLiLv = table.selectXpath("tbody/tr[14]/td[2]").text();


        String dingHuoLiangBian3YueLiLv = table.selectXpath("tbody/tr[16]/td[2]").text();
        String dingHuoLiangBian6YueLiLv = table.selectXpath("tbody/tr[17]/td[2]").text();
        String dingHuoLiangBian12YueLiLv = table.selectXpath("tbody/tr[18]/td[2]").text();

        String tongZhiCunKuan1TianLiLv = table.selectXpath("tbody/tr[21]/td[2]").text();
        String tongZhiCunKuan7TianLiLv = table.selectXpath("tbody/tr[22]/td[2]").text();

        String xieDingCunKuanLiLv = null;

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "广发银行";
    }

    private String getKaiShiZhiXingRiQi(String input) {
        Pattern pattern = Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2})");
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }
}
